package com.eien.phonezwei.utils;

import android.Manifest;
import android.content.ContentResolver;
import android.content.Context;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.provider.CallLog;
import android.provider.ContactsContract;
import android.telephony.SmsManager;
import android.widget.Toast;

import java.util.ArrayList;


/**
 * Created by honooon on 2016.3.07.
 */
public class ZUtil {

    /**
     * 发送短信的方法
     *
     * @param sendToNumber 接收人手机号
     * @param msg          短信内容
     */
    public static void sendSMS(Context context, String sendToNumber, String msg) {
        if (msg != null) {
            SmsManager sms = SmsManager.getDefault();
            ArrayList<String> texts = sms.divideMessage(msg);
            if (texts.size() <= 1) {
                sms.sendTextMessage(sendToNumber, null, msg, null, null);
            } else if (texts.size() > 1) {
                sms.sendMultipartTextMessage(sendToNumber, null, texts, null, null);
            }
            Toast.makeText(context, "转发至: " + sendToNumber, Toast.LENGTH_SHORT).show();
        }
    }

    /**
     * 读取新（未处理）未接来电数量
     *
     * @param context Context
     * @return 新未接来电数量
     */
    public static int readMissedCall(Context context) {
        int result = 0;
        if (context.checkCallingOrSelfPermission(Manifest.permission.READ_CALL_LOG) == PackageManager.PERMISSION_GRANTED) {
            Cursor cursor = context.getContentResolver().query(CallLog.Calls.CONTENT_URI, new String[]{CallLog.Calls.TYPE}, " type=? and " +
                    "new=?", new String[]{CallLog.Calls.MISSED_TYPE + "", "1"}, "date desc");
            if (cursor != null) {
                result = cursor.getCount();
                cursor.close();
            }
        }
        return result;
    }


    /**
     * 未接来电号码、名字信息生成发送短信的字符串
     *
     * @param context Context
     * @return 短信正文字符串
     */
    public static String missedCallToString(Context context) {
        String result = "";
        if (context.checkCallingOrSelfPermission(Manifest.permission.READ_CALL_LOG) == PackageManager.PERMISSION_GRANTED) {
            Cursor cursor = context.getContentResolver().query(CallLog.Calls.CONTENT_URI, new String[]{CallLog.Calls.NUMBER, CallLog
                    .Calls.CACHED_NAME, CallLog.Calls.TYPE}, " type=? and new=?", new String[]{CallLog.Calls.MISSED_TYPE + "", "1"},
                    "date desc");
            if (cursor != null) {
                while (cursor.moveToNext()) {
                    String numb = cursor.getString(cursor.getColumnIndexOrThrow(CallLog.Calls.NUMBER));
                    String name = cursor.getString(cursor.getColumnIndexOrThrow(CallLog.Calls.CACHED_NAME));
                    if (name != null) {
                        numb = name + "(" + numb + ")";
                    }
                    if (result.equals("")) {
                        result = numb;
                    } else {
                        result = result + "， " + numb;
                    }
                }
                cursor.close();
            }
        }
        return result;
    }

    /**
     * 根据号码从通讯录得到联系人信息
     *
     * @param context  Context
     * @param phoneNum 手机号码
     * @return 通讯录联系人姓名
     */
    public static String getContactNameFromPhoneNum(Context context, String phoneNum) {
        String contactName = "";
        ContentResolver cr = context.getContentResolver();
        try {
            Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone
                    .NUMBER + " = ?", new String[]{phoneNum}, null);
            if (pCur != null) {
                if (pCur.moveToFirst()) {
                    contactName = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                    pCur.close();
                }
            }

        } catch (Exception e) {
            System.out.println(e.toString());
        }

        return contactName;
    }
}
